<?php
require('src/OrdenCompra.class.php');
require('src/Producto.class.php');

$id   	= (isset($_GET['id'])) ? $_GET['id']:0;
$cliId  = (isset($_GET['cliId'])) ? $_GET['cliId']:0;
?>

<script  type="text/javascript">
var id              = <?php echo $id; ?>;
var cliId           = <?php echo $cliId; ?>;
var tipoInsertar    = 0;
var objTabla        = {};
var i               = 0;
var iGlobal         = 0;
var esAdd           = false;
var data            = {  prod   : "",
                         prodId : 0,
                         cant   : 0,
                         idEle  : 0,
                         iocId  : 0
                        };
var ioc             = null;
$(document).ready(function(){
    $("#prod").autocomplete({
        source: "src/producto.php?modo="+6+"&tipoProducto=2",
        minLength: 2,
        select: function(event, ui) {
            $("#prodId").val(ui.item.id);
        }
    });
	$('#agregarOC').bind('click',  agregarRowModificar  );
	
}); 

function deleteElemento(id){
    var vector = id.split("eliminar");
    var elemento = objTabla[vector[0]+"elemento"] ;
    var vectorElemento = elemento.split("#");
    var prod= vectorElemento[3];
    var prodId= vectorElemento[1];
    var cant= vectorElemento[2];
    var idEle= vectorElemento[0];
    var idIoc= vectorElemento[5];

    agregarObj( vector[0], prodId, 	cant,	 prod, 3, 0, idIoc);
}
function modificarElemento(id){
    var vector = id.split("modificar");
    iGlobal = vector[0];
    var elementoModificar = objTabla[vector[0]+"elemento"].split("#");
    $('#prod').val(elementoModificar[3]);
    $('#prodId').val(elementoModificar[1]);
    $('#cant').val(elementoModificar[2]);
    $('#idElemento').val(elementoModificar[0]);
    $('#idIoc').val(elementoModificar[5]);
    $('#agregarOC').unbind();
    $('#agregarOC').bind('click', modificar);
}

   
function limpiarForm(){
    $("#prod").val("");
    $("#prodId").val(0);
    $("#cant").val("");
    $('#idElemento').val(0);
    $('#idIoc').val(0);
}
function agregarRow(){
    
    if(id==0){
        if($('#formID1').validationEngine('validate')){
            data = { prod   : $('#prod').attr('value'),
                     prodId : $('#prodId').attr('value'),
                     cant   : $('#cant').attr('value'),
                     idEle  : $('#idElemento').attr('value'),
                     iocId  : $('#idIoc').attr('value'),
					 accion : 1
                    };
            agregar(data);
        }
    }else{
        //alert(ifvArray.toSource());
        for(var x = 0, n = ifvArray.length; x < n ; x++){
            data = { prod   : ifvArray[x].pte_nombre,
                     prodId : ifvArray[x].pte_id,
                     cant   : ifvArray[x].ifv_cantordenada,
                     idEle  : 0,
                     iocId  : ifvArray[x].ifv_id,
		     accion : 0
                    };
            $('#idIoc').val(ifvArray[x].ifv_id);
            agregar(data);

        }
    }
    
}

function agregar (data) {

    i++;
    agregarObj(i, data.prodId, data.cant, data.prod, data.accion , data.idEle, data.iocId);
    var tds = "<tr id ='tr"+i+"'><td>"+i+"</td>"+
                      "<td>"+data.prod+"</td>"+
                      "<td>"+data.cant+"</td>"+
                      "<td><img src='public/images/shared/icon-delete-form.png'  id='"+i+"eliminar' '/> |<img src='public/images/shared/icon-edit-form.png'  id='"+i+"modificar' /> </td></tr>";

    limpiarForm();
    $("#tbl  > tbody:last").append(tds);
    activarBotones(i);

}

function modificar() {
    var prod    = $('#prod').attr('value');
    var prodId  = $('#prodId').attr('value');
    var cant    = $('#cant').attr('value');
    var idEle   = $('#idElemento').attr('value');
    var idIoc   = $('#idIoc').attr('value');
    agregarObj(iGlobal, prodId, cant, prod, 2, 0, idIoc);
    var tds = " <td>"+iGlobal+"</td>"+
                      "<td>"+prod+"</td>"+
                      "<td>"+cant+"</td>"+
                      "<td >"+
                            "<img src='public/images/shared/icon-delete-form.png'  id='"+iGlobal+"eliminar' '/>"+
                            "|<img src='public/images/shared/icon-edit-form.png'  id='"+iGlobal+"modificar' /> "+               
                      "</td>";

    $("#tr"+iGlobal).html(tds);  
     $('#agregarOC').unbind();
     limpiarForm();
     activarBotones(iGlobal);
    $('#agregarOC').bind('click',  agregar  );
}
function activarBotones(i){
    $('#'+i+'eliminar').bind('click', function() {
        deleteElemento(this.id);
        $('#tr'+i).remove();
    });
    $('#'+i+'modificar').bind('click', function() {
        modificarElemento(this.id);
    });
}

function agregarObj (i,prodId,cant,prod,accion,idEle, idIoc){
    var idElemento = (tipoInsertar==0)? 0:idEle;
    //alert("--> "+idElemento+"#"+prodId+"#"+cant+"#"+prod+"#"+accion+"#"+idIoc);
    objTabla[i+"elemento"] = idElemento+"#"+prodId+"#"+cant+"#"+prod+"#"+accion+"#"+idIoc;

}

function agregarRowModificar(){
    
        if($('#formID1').validationEngine('validate')){
            data = { prod   : $('#prod').attr('value'),
                     prodId : $('#prodId').attr('value'),
                     cant   : $('#cant').attr('value'),
                     idEle  : $('#idElemento').attr('value'),
                     iocId  : $('#idIoc').attr('value'),
					 accion :1
                    };
            agregar(data);
   

        }
    
    
}
</script>
<div id="stylized" class="myform" style="margin:20px auto;">

<div id="material_comprado"  > </div>  
    
    <h2>Orden Factura</h2>
    <table>
    <tr>
        <td><span class="title">R.I.F.: </span><span id="lblRif" class="text"></span></td>
        <td><span class="title">Raz&oacute;n Social: </span><span id="lblRazonS" class="text"></span></td>
    </tr>
    <tr>
        <td><span class="title">Direcci&oacute;n Fiscal: </span><span id="lblDireccion" class="text"></span></td>
        <td></td>
    </tr>
    <tr>
        <td><span class="title">T&eacute;lefono: </span><span id="lblTelefono" class="text"></span></td>
        <td></td>
    </tr>
    </table>
    <p></p>
    <div id="form-add">
        <form id="formID1">
            <label><span class="required">*</span>Producto<span class="small">Producto Terminado</span></label>
            <input id="prod" type="text"  name="fields[]"  style="width:200px;" class="validate[required] text-input" />
            <input type='hidden' id="prodId"  name="fieldsId[]" value="" />            
            <span  style="float:left;padding: 8px 0px 8px 8px;font-weight: bold;"><span class="required">*</span>Cantidad Ordenada</span> 
            <input id="cant" type="text" name="fieldscantidad[]"  style="width:40px;" class="validate[required, number] text-input"/>
            <input class="bt_plus" id="agregarOC" type="button" value="Agregar"/>
            <input type="hidden" name="idElemento" id="idElemento" />
            <input type="hidden" name="idIoc" id="idIoc" />
        </form>
    </div>
    </br>
    <table class="doc" id="tbl" >
        <thead>
            <tr>
                <th>&Iacute;tem</th>
                <th>Producto</th>
                <th>Cantidad Ordenada</th>
                <th>#</th>
            </tr>            
        </thead>
        <tbody>
            
        </tbody>
    </table>
</div>

</div>

